<template>
	<div class="w-full my-3 pb-8 flex items-start justify-start gap-2 h-full">
		<div class="bg-white p-4 rounded w-full h-full">
			<!-- 通道列表渲染区 -->
			<div class="grid grid-cols-5 flex items-center">
				<template v-if="channelList.length">
					<div v-for="item in channelList" :key="item.key" class="flex p-6" @click="onInfoChannel(item)">
						<!---图标居左-->
						<div v-if="item.icon" class="w-1/5 flex-shrink-0 flex items-center justify-center">
							<SvgIcon :name="item.icon" :iconify="true" v-if="item.icon" class="mb-2 w-6 h-6 iconify iconify--uiw" :class="{'active':state.queryForm.channel==item.key}"/>
						</div>
						<div class="w-4/5 flex flex-col">
							<!-- 标题右上 -->
							<div class="text-lg font-semibold text-gray-800"  :class="{'active':state.queryForm.channel==item.key}">
								{{ item.label }}
							</div>
							<!-- 内容居右下 -->
							<div class="text-sm text-gray-600">
								{{ item.info }}
							</div>
						</div>
					</div>
				</template>
            </div>
			<el-row>
				<div class="mb8" style="width: 100%">
					<el-button v-auth="'aigc_app_api_add'" @click="handelSubmit" class="ml10" icon="folder-add" type="primary">
						{{ t('aigcAppApi.createApiKey') }}
					</el-button>

					<el-button
						plain
						v-auth="'aigc_app_api_delete'"
						:disabled="multiple"
						@click="handleDelete(selectObjs)"
						class="ml10"
						icon="Delete"
						type="primary"
					>
						{{ t('common.delBtn') }}
					</el-button>

					<right-toolbar
						@queryTable="getDataList"
						class="ml10"
						style="float: right; margin-right: 20px"
						v-model:showSearch="showSearch"
					></right-toolbar>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				@selection-change="handleSelectionChange"
				style="width: 100%"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
			>
				<el-table-column align="center" type="selection" width="40" />
				<el-table-column :label="t('aigcModel.apiKey')" prop="apiKey" show-overflow-tooltip />
				<el-table-column :label="t('param.createTime')" prop="createTime" show-overflow-tooltip />
				<el-table-column :label="$t('common.action')" width="150">
					<template #default="scope">
						<el-button icon="copy" @click="handelCopy(scope.row)" text type="primary" v-auth="'aigc_app_api_edit'"
							>{{ $t('common.copyBtn') }}
						</el-button>

						<el-button
							icon="delete"
							v-auth="'aigc_app_api_delete'"
							@click="handleDelete([scope.row.id])"
							text
							type="primary"
						>
							{{ $t('common.delBtn') }}
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination @current-change="currentChangeHandle" @size-change="sizeChangeHandle" v-bind="state.pagination" />
		</div>
	</div>
</template>

<script lang="ts" name="aigcAppApi" setup>
import { useI18n } from 'vue-i18n';
import { addObj, delObj, pageList } from '/@/api/aigc/appApi';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { BasicTableProps, useTable } from '/@/hooks/table';
import { useAppStore } from '/@/stores/aigc/appStore';
import { copyToClipboard } from '/@/utils/copy';
// 引入组件
const { t } = useI18n();
//渠道信息
 const channelList=[
	 	{key:'apiChannel',icon:'hugeicons:api',label:t('aigcAppApi.apiChannel'),info:t('aigcAppApi.apiChannelInfo')},
        {key:'webChannel',icon:'streamline-plump:web-remix',label:t('aigcAppApi.webChannel'),info:t('aigcAppApi.webChannelInfo')},
        {key:'wxChannel',icon:'streamline-logos:wechat-logo-block',label:t('aigcAppApi.wxChannel'),info:t('aigcAppApi.wxChannelInfo')},
		{key:'officalAccountChannel',icon:'mingcute:wechat-miniprogram-line',label:t('aigcAppApi.officalAccountChannel'),info:t('aigcAppApi.officalAccountChannelInfo')},
		{key:'feishuChannel',icon:'arcticons:bhim',label:t('aigcAppApi.feishuChannel'),info:t('aigcAppApi.feishuChannelInfo')},
		{key:'dingdingChannel',icon:'ri:dingding-line',label:t('aigcAppApi.dingdingChannel'),info:t('aigcAppApi.dingdingChannelInfo')}
    ];
// 搜索变量
const queryRef = ref();
const showSearch = ref(true);
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);
const appStore=useAppStore();

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {
		channel: 'apiChannel',
		appId:appStore?.info?.id||"",
	},
	pageList: pageList,
	descs: ['create_time'],
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	queryRef.value.resetFields();
	getDataList();
};

// 多选事件
const handleSelectionChange = (objs: { id: string }[]) => {
	selectObjs.value = objs.map(({ id }) => id);
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm(t('common.delConfirmText'));
	} catch {
		return;
	}

	try {
		await delObj(ids);
		getDataList();
		useMessage().success(t('common.delSuccessText'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
//点击
const onInfoChannel=(item:any)=>{
	state.queryForm.channel=item.key;
	getDataList();
}
//新增密钥 
const handelSubmit=()=>{
	const data={
		appId:appStore.info.id,
		channel:state.queryForm.channel,
	};
	addObj(data).then((res)=>{
		if(res.success){
			useMessage().success(t('common.addSuccessText'));
			getDataList();
		}else{
			useMessage().error(res.message||t('aigcAppApi.addApiKeyFailure'));
		}
	});
}
//复制
const handelCopy=(item:any)=>{
	copyToClipboard(item.apiKey);
	useMessage().info(t('aigcAppApi.copyApiKeySuccess'));
}

</script>

<style lang="css" scoped>
	.active{
		background:bule;
		opacity: 60%;
		color: blue;
	}
</style>